﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;

namespace Cars
{    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<CarInfo> cars = new List<CarInfo>();

            for (int page = 0; page < 1; page++)
            {
                string response = WebUtils.HttpGet(string.Format("http://www.ksl.com/index.php?sid=&nid=443&tab=list&pid={0}", page));               

                int i = 0;
                while (true)
                {
                    TextBlock carBlock = CarInfo.getTextBlock(response, i, "<div class=\"adBox\">", false);

                    if (carBlock.StartIdx == -1)
                    {
                        break;
                    }

                    // do some stuff
                    KslCarInfo car = new KslCarInfo();
                    car.ParseListView(carBlock.Text);
                    cars.Add(car);

                    // make sure we look for the next one
                    i = carBlock.EndIdx;
                }                
            }

            dataGridView1.DataSource = cars;

            foreach (CarInfo thisCar in cars)
            { 
                if ((string.Compare(thisCar.Make, "Honda", true) == 0) &&
                   (string.Compare(thisCar.Model, "Civic", true) == 0))
                {
                    string message = string.Format("{0} {1} {2} {3:C}", thisCar.Year, thisCar.Make, thisCar.Model, thisCar.Price);
                    WebUtils.SmtpSend(new string[1] {"nbayles@hotmail.com"},
                        "nbayles@hotmail.com", "KSL Alert", message, "localhost", 25, "blackd6@gmail.com", "yourmomisgay");
                }

            }
        }

             
       
    }
}
